package cn.sunshine.basic.service.impl;

import cn.sunshine.basic.mapper.BaseMapper;
import cn.sunshine.basic.query.BaseQuery;
import cn.sunshine.basic.service.IBaseService;
import cn.sunshine.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)

public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    /*
     * @Title: loadAll
     * @Description: 这个方法是查找所有
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/21 16:59
     * @Parameters: []
     * @Return java.util.List<cn.sunshine.org.domain.T>
     */
    @Override
    public List<T> loadAll( ) {
        return baseMapper.loadAll();
    }

    /*
     * @Title: loadById
     * @Description: 根据主键ID查找对象
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/21 17:00
     * @Parameters: [id]
     * @Return cn.sunshine.org.domain.T
     */
    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    /*
     * @Title: remove
     * @Description: 根据ID删除对象
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/21 17:01
     * @Parameters: [id]
     * @Return void
     */
    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /*
     * @Title: save
     * @Description: 保存对象
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/21 17:01
     * @Parameters: [t]
     * @Return void
     */
    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    /*
     * @Title: update
     * @Description: 修改对象
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/21 17:02
     * @Parameters: [t]
     * @Return void
     */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    /*
     * @Title: queryData
     * @Description: 高级查询相关的方法
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/21 17:02
     * @Parameters: [tQuery]
     * @Return cn.sunshine.basic.utils.PageList<cn.sunshine.org.domain.T>
     */
    @Override
    public PageList<T> queryData(BaseQuery baseQuery) {

        // 1.查询分页总条数
        Integer total = baseMapper.queryCount(baseQuery);
        // 2.查询当前页数据
        List<T> rows = baseMapper.queryData(baseQuery);
        return new PageList<>(total, rows);
    }
    /*
     * @Title: batchT
     * @Description: 根据id批量删除部门数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/25 0:18
     * @Parameters: [ids]
     * @Return void
     */
    @Override
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }

}
